import pygame
import tile_tools as tt
import math as Math
class Projectile:
	def __init__(self,type,damage,user,trans,range,dirx,diry,x,y):
		self.trans = trans
		self.x = x
		self.y = y
		self.g = 0
		self.damage = damage
		self.user = user
		self.range = range
		self.dirx = dirx
		self.diry = diry
		self.cur_range = 0
		self.type = type
		self.proj_surface = 0
		if type == "rock":
			self.proj_surface = pygame.image.load("resources/graphics/projectile/rock.bmp").convert()
			self.proj_surface = tt.get_tile_graphic(0,self.proj_surface.get_width(), self.proj_surface.get_height(),self.proj_surface)
		elif type == "bullet":
			self.proj_surface = pygame.image.load("resources/graphics/projectile/bullet.bmp").convert()
		elif type == "fireball":
			self.proj_surface = pygame.image.load("resources/graphics/projectile/fireball.bmp").convert()
		elif type == "acid":
			self.proj_surface = pygame.image.load("resources/graphics/projectile/acid.bmp").convert()
			self.proj_surface = tt.get_tile_graphic(0,self.proj_surface.get_width(), self.proj_surface.get_height(),self.proj_surface)
		self.w = self.proj_surface.get_width()
		self.h = self.proj_surface.get_height()
	
	def move(self):
		if self.cur_range > self.range:
			return 0
		else:
			if self.type <> "acid":
				self.cur_range = self.cur_range + 1
				self.x = self.x + self.dirx
				self.y = self.y + self.diry
				return 1
			else:
				self.x = self.x + self.dirx*self.g
				self.g = self.g + 0.2
				if self.g > 3:
					self.y = self.y - self.diry*self.g
					self.g = self.g - 0.2
				else:
					self.y = self.y + self.diry*self.g
					self.g = self.g + 0.2
				return 1
			
	def draw(self):
		return self.proj_surface
		
	def check_collision(self,map,ply):
		c = 0
		B1 = pygame.Rect((self.x,self.y),(self.w,self.h))
		if self.user == "Player":
			for i in range(0,len(map.elist)):
				if map.elist[i].is_dead(ply) == False:
					B2 = pygame.Rect((map.elist[i].x,map.elist[i].y),(map.elist[i].w,map.elist[i].h))
					if B1.colliderect(B2):
						map.elist[i].health = map.elist[i].health - self.damage
						return 1
		elif self.user == "Enemy":
			B2 = pygame.Rect((ply.x,ply.y),(32,64))
			if B1.colliderect(B2):
				ply.health = ply.health - self.damage
				return 1
		if self.trans == False:
			xrange = int(Math.floor(map.dx/32))
			yrange = abs(int(Math.floor(map.dy/32)))
			for i in range(yrange,yrange+20):
				for j in range(xrange, xrange+26):
					if i >= map.reference.get_height() or j >= map.reference.get_width():
						break;
					if map.maparray[i][j] is not -1:
						#B1 = pygame.Rect((self.x,self.y),(self.w,self.h))
						B2 = pygame.Rect((j*32,i*32),(32,32))
						if B1.colliderect(B2):
							return 1
					c = c + 1
		return 0
			